package com.easy.controller;

import com.easy.bean.Reservations;
import com.easy.bean.Rooms;
import com.easy.common.CommonResult;
import com.easy.dao.IRoomsDao;
import com.easy.service.IReservations;
import com.easy.service.IRooms;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class RoomsController {
    @Autowired
    IRoomsDao dao;

    @Autowired
    IRooms easyA;

    @Autowired
    IReservations easyR;

    //查询
    @GetMapping("searchrooms")
    public CommonResult getSearchrooms(Rooms rooms, Page page){
        List<Rooms> list=easyA.getSearchRooms(rooms,page);
        int count=easyA.getCount(rooms);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);

        return CommonResult.success(map);
    }

    @GetMapping("free-rooms")
    public CommonResult findfreeRooms(Rooms rooms){
        List<Rooms> list=dao.findfreeRooms(rooms);
        return CommonResult.success(list);
    }

    //添加
    @PostMapping("roomsjson")
    public CommonResult addRoomsJson(@RequestBody Rooms rooms){
        dao.addRooms(rooms);
        rooms =easyA.getRooms(rooms.getId());
        return CommonResult.success(rooms);
    }

    @GetMapping("rooms")
    public CommonResult getSearchRooms(Rooms rooms){
        List<Rooms> list=dao.getSearchRooms(rooms);
        return CommonResult.success(list);
    }
    //删除
    @DeleteMapping("rooms/{id}")
    public CommonResult delRooms(@PathVariable int id){
        int count= easyA.delRooms(id);
//        return CommonResult.success(rooms);
        if (count>0){
            return CommonResult.success(count);
        }else{
            return CommonResult.fail();
        }
    }

    //修改
    @PutMapping("roomsjson")
    public CommonResult editRoomsJson(@RequestBody Rooms rooms){
        int count=easyA.editRooms(rooms);
        rooms=easyA.getRooms(rooms.getId());
        return CommonResult.success(rooms);
    }

    @PutMapping("norooms")
    public CommonResult editRoomsno(@RequestBody Rooms rooms){
        int count=easyA.editRooms_check(rooms);
        rooms=easyA.getRooms(rooms.getId());
        return CommonResult.success(rooms);
    }


    @PutMapping("rooms_check")
    public CommonResult editRoomsJson_check(Rooms rooms,Reservations reservations){
        easyR.editReservations_room(reservations);
        int count=easyA.editRooms_check(rooms);
        rooms=easyA.getRooms(rooms.getId());
        return CommonResult.success(rooms);
    }

    @GetMapping("status-counts")
    public Map<String, Integer> getRoomStatusCounts(Rooms rooms) {
        return easyA.getRoomStatusCounts(rooms);
    }
}
